import { getUserInfo, logout } from '@/api/user'

const user = {
    namespaced: true,
    state: () => ({
        token: null,
        info: {},
    }),
    mutations: {
        setUser(state, info) {
            state.info = info
        },
        setToken(state, token) {
            state.token = token
        },
        clear(state) {
            state.token = null
            state.info = {}
        },
    },
    actions: {
        signout: async (context) => {
            logout()
            context.commit('clear')
        },
        setUser: async (context) => {
            const resp = await getUserInfo()
            if (resp.status === 200) {
                context.commit('setUser', resp.data)
            }
        },
    },
}

export default user
